﻿using System;
using System.Text;
using NUnit.Framework;

namespace MusicLyricAppTest.Utils
{
    [TestFixture]
    public class XmlUtils
    {
        [Test]
        public void TestParseQQVerbatim()
        {
            string text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<QrcInfos>\n    <QrcHeadInfo SaveTime=\"1411719278\" Version=\"100\"/>\n    <LyricInfo LyricCount=\"1\">\n        <Lyric_1 LyricType=\"1\" LyricContent=\"[ti:Right Above It (Made Famous by Lil Wayne feat. Drake)]\n[ar:Future Hit Makers]\n[al:Blockbuster Hits]\n[by:]\n[offset:0]\n[0,18820]Right(0,672) (672,672)Above(1344,672) (2016,672)It(2688,672)((3360,672)Made(4032,672) (4704,672)Famous(5376,672) (6048,672)by(6720,672) (7392,672)Lil(8064,672) (8736,672)Wayne(9408,672) (10080,672)feat.(10752,672) (11424,672)Drake(12096,672))(12768,672) (13440,672)-(14112,672) (14784,672)Future(15456,672) (16128,672)Hit(16800,672) (17472,672)Makers(18144,672)\n[18820,1500]Now (18820,220)tell (19040,210)me (19250,210)how (19460,190)ya (19650,190)love (19840,230)it(20070,250)\n[20320,1340]You (20320,210)know (20530,190)you (20720,210)at (20930,190)the (21120,170)top (21290,180)and(21470,609)\n[22079,1250]All (22079,340)of (22419,180)heaven's (22599,180)right (22779,190)above (22969,170)it'(23139,190)\n[23329,4100]We (23329,200)on   (23529,1830)Cuz (25359,200)we (25559,210)on  (25769,1660)\n[27429,2410]Who (27429,210)else (27639,240)really (27879,260)tryin' (28139,250)to (28389,220)f**k (28609,240)with (28849,220)Hollywood (29069,220)Co  (29289,550)\n[29839,1710]I'm (29839,360)with (30199,240)Marley (30439,230)G  (30669,430)bro(31099,450)\n[31549,3360]Flyin' (31549,260)Hollygrove (31809,510)chicks (32319,340)to (32659,420)my (33079,280)Hollywood (33359,660)shows(34019,890)\n[34909,1540]And (34909,230)I (35139,210)wanna (35349,210)tell (35559,220)you (35779,200)somethin'(35979,470)\n[36449,1400]That (36449,230)you (36679,220)probably (36899,200)should (37099,320)know(37419,430)\n[37849,1660]This (37849,240)that (38089,210)\"Slumdog (38299,440)millionaire\"(38739,770)\n        [39509,3169]Bollywood (39509,899)flow (40408,470)and (40878,310)uh  (41188,1490)\n        [42678,2210]My (42678,230)real (42908,250)friends (43158,350)never (43508,240)hear (43748,330)it (44078,250)from (44328,270)me(44598,290)\n        [44888,1730]Fake (44888,210)friends (45098,200)write (45298,200)the (45498,210)wrong (45708,240)answers(45948,670)\n        [46618,1310]On (46618,280)the (46898,180)mirror (47078,260)for (47338,220)me(47558,370)\n        [47928,1430]That's (47928,260)why (48188,220)I (48408,250)pick (48658,240)and (48898,200)choose(49098,260)\n        [49358,1270]I (49358,330)don't (49688,230)get (49918,230)sh*t (50148,220)confused(50368,820)\n        [51188,1420]I (51188,210)got (51398,190)a (51588,220)small (51808,310)circle(52118,490)\n        [52608,1700]I'm (52608,280)not (52888,230)with (53118,340)different (53458,250)crews(53708,600)\n        [54308,1480]We (54308,240)walk (54548,220)the (54768,260)same (55028,250)path'(55278,510)\n        [55788,1350]But (55788,310)got (56098,230)on (56328,210)different (56538,240)shoes(56778,360)\n        [57138,1750]Live (57138,450)in (57588,370)the (57958,250)same (58208,210)buildin'(58418,470)\n        [58888,1480]But (58888,320)we (59208,230)got (59438,260)different (59698,290)views(59988,380)\n        [60368,3390]I (60368,400)got (60768,230)a (60998,280)couple (61278,240)cars' (61518,580)I (62098,250)never (62348,290)get (62638,250)to (62888,240)use(63128,630)\n        [63758,1390]Don't (63758,220)like (63978,230)my (64208,240)women (64448,230)single(64678,470)\n        [65148,2360]I (65148,280)like (65428,230)my (65658,210)chicks (65868,210)in (66078,270)twos(66348,1160)\n        [67508,2740]And (67508,220)these (67728,230)days (67958,290)all (68248,240)the (68488,200)girls (68688,230)is (68918,210)down (69128,220)to (69348,210)roll(69558,690)\n        [70248,3060]I (70248,210)hit (70458,230)the (70688,230)strip (70918,260)club (71178,200)and (71378,230)all (71608,210)them (71818,230)b**ches (72048,210)find (72258,240)a (72498,230)pole(72728,559)\n        [73287,1010]Plus (73287,250)I've (73537,220)been (73757,260)sippin'(74017,280)\n        [74297,1620]So (74297,260)this (74557,250)sh*t (74807,220)is (75027,210)movin' (75237,180)kinda (75417,230)slow(75647,270)\n        [75917,1800]Just (75917,400)tell (76317,330)my (76647,240)girl (76887,240)to (77127,260)tell(77387,330)\n        [77717,3140]Her (77717,210)friend (77927,190)that (78117,230)it's (78347,230)time (78577,210)to (78787,210)go(78997,1860)\n        [80857,1600]Now (80857,210)tell (81067,270)me (81337,240)how (81577,220)you (81797,210)love (82007,180)it(82187,270)\n        [82457,1650]You (82457,200)know (82657,200)you (82857,190)at (83047,200)the (83247,180)top (83427,220)and (83647,180)all(83827,280)\n        [84107,2650]Of (84107,190)heaven's (84297,200)right (84497,200)above (84697,210)it (84907,220)we (85127,230)on  (85357,1400)\n        [86757,1250]It's (86757,190)young (86947,210)money (87157,270)motherf**ker(87427,580)\n        [88007,2080]If (88007,730)you (88737,270)ain't (89007,320)runnin' (89327,230)wit(89557,530)\n        [90087,1560]It (90087,330)run (90417,260)from (90677,200)it (90877,190)motherf**ker (91067,650)alright(91717,620)\n        [92337,2490]Now (92337,250)somebody (92587,280)show (92867,340)some (93207,270)money (93477,450)in (93927,220)this (94147,240)b**ch(94387,440)\n        [94827,1630]And (94827,220)I (95047,230)got (95277,210)my (95487,200)bees (95687,220)with (95907,200)me(96107,350)\n        [96457,2990]Like (96457,220)some (96677,200)honey (96877,220)in (97097,240)this (97337,190)b**ch' (97527,210)ya (97737,240)dig  (97977,1470)\n        [99447,1910]I (99447,220)got (99667,210)my (99877,230)gun (100107,190)in (100297,220)my (100517,190)boo (100707,180)purse(100887,470)\n        [101357,4919]And (101357,210)I (101567,250)don't (101817,230)bust (102047,220)back' (102267,250)because (102517,629)I (103146,280)shoot (103426,420)first  (103846,2430)\n        [106276,1650]Meet (106276,230)me (106506,210)on (106716,190)the (106906,190)fresh (107096,210)train(107306,620)\n        [107926,1100]Yes (107926,210)I'm (108136,200)in (108336,210)the (108546,190)buildin'(108736,290)\n        [109026,1880]You (109026,210)just (109236,180)on (109416,210)the (109626,220)list (109846,190)of (110036,210)guest (110246,290)names(110536,370)\n        [110906,3120]And (110906,240)all (111146,200)my (111346,200)riders (111546,220)do (111766,210)not (111976,220)give (112196,190)a (112386,250)f**k: (112636,570)X-Games(113206,820)\n        [114026,4460]Guns (114026,280)turn (114306,320)you (114626,210)boys (114836,370)into (115206,240)pussies: (115446,820)sex (116266,380)change (116646,1840)\n        [118486,1930]And (118486,230)I (118716,230)smoke (118946,220)til' (119166,220)I (119386,200)got (119586,210)chest (119796,200)pains(119996,420)\n        [120416,1230]And (120416,230)you (120646,230)niggas (120876,240)know(121116,530)\n        [121646,1960]I (121646,200)rep (121846,220)my (122066,230)gang (122296,210)like (122506,240)Jesse (122746,230)James(122976,630)\n        [123606,830]Women (123606,250)are (123856,310)possessive'(124166,820)\n        [124986,1750]And (124986,230)they (125216,230)wanna (125446,240)possess (125686,240)Wayne(125926,810)\n        [126736,1140]I (126736,230)been (126966,210)flyin' (127176,240)so (127416,180)long(127596,280)\n        [127876,1570]I (127876,260)fell (128136,200)asleep (128336,220)on (128556,230)the (128786,210)f**kin (128996,220)plane(129216,1790)\n        [131006,1770]Skinny (131006,260)pants (131266,330)and (131596,400)some (131996,410)vans(132406,370)\n        [132776,1770]Call (132776,380)me (133156,310)Triple-A (133466,230)get (133696,320)my (134016,220)advance(134236,839)\n        [135075,1620]In (135075,240)advance' (135315,410)Amen(135725,970)\n        [136695,2480]As (136695,430)the (137125,240)world (137365,170)spin (137535,220)and (137755,230)dance (137985,230)in (138215,220)my (138435,220)hands(138655,520)\n        [139175,2740]Life (139175,280)is (139455,290)a (139745,260)beach' (140005,270)I'm (140275,230)just (140505,350)playin' (140855,280)in (141135,240)the (141375,250)sand(141625,1390)\n        [143015,2550]Ugh  (143015,830)Wake (143845,230)up (144075,240)and (144315,220)smell (144535,230)the (144765,220)p**sy(144985,580)\n        [145565,3049]You (145565,240)niggas (145805,230)can't (146035,200)see (146235,200)me' (146435,449)but (146884,250)never (147134,200)overlooked (147334,650)me(147984,630)\n        [148614,1840]I'm (148614,280)on (148894,210)the (149104,240)paper (149344,240)trail (149584,240)there (149824,260)ain't(150084,370)\n        [150454,1810]No (150454,260)tellin' (150714,190)where (150904,230)it (151134,210)took (151344,210)me' (151554,510)yeah(152064,830)\n        [152894,3780]And (152894,260)I (153154,260)ain't (153414,210)a (153624,220)killer' (153844,210)but (154054,170)don't (154224,210)push (154434,190)me(154624,2050)\n        [156674,1820]Now (156674,210)tell (156884,210)me (157094,230)how (157324,240)you (157564,240)love (157804,380)it(158184,310)\n        [158494,1690]You (158494,200)know (158694,210)you (158904,200)at (159104,190)the (159294,230)top (159524,180)and (159704,190)all(159894,290)\n        [160184,2610]Of (160184,220)heaven's (160404,210)right (160614,220)above (160834,240)it (161074,220)we (161294,250)on  (161544,1250)\n        [162794,2900]It's (162794,230)young (163024,240)money (163264,220)motherf**ker (163484,220)if (163704,330)you (164034,270)ain't (164304,240)runnin' (164544,290)wit(164834,860)\n        [165694,1870]It (165694,290)run (165984,350)from (166334,280)it (166614,250)motherf**ker (166864,210)alright(167074,490)\n        [167564,3000]Now (167564,410)somebody (167974,350)show (168324,320)some (168644,340)money (168984,620)in (169604,290)this (169894,290)b**ch(170184,380)\n        [170564,2590]And (170564,240)I (170804,290)got (171094,280)my (171374,220)bees (171594,230)with (171824,210)me (172034,210)like (172244,210)some (172454,220)honey(172674,569)\n        [173243,2020]In (173243,220)this (173463,170)b**ch' (173633,180)ya (173813,330)dig  (174143,1120)\n        [175263,2030]I (175263,230)got (175493,220)my (175713,250)gun (175963,220)in (176183,230)my (176413,220)boo (176633,220)purse(176853,440)\n        [177293,3930]And (177293,220)I (177513,250)don't (177763,220)bust (177983,250)back' (178233,210)because (178443,220)I (178663,230)shoot (178893,470)first  (179363,1860)\n        [181223,2380]Ugh  (181223,380)how (181603,370)do (181973,210)he (182183,220)say (182403,230)what's (182633,230)never (182863,230)said (183093,510)\n        [183603,1589]Beautiful (183603,659)black (184262,410)women(184672,520)\n        [185192,1700]I (185192,240)bet (185432,200)that (185632,200)b**ch (185832,220)look (186052,210)better (186262,180)red(186442,450)\n        [186892,1020]Limpin' (186892,240)off (187132,370)tour(187502,410)\n        [187912,2110]Cuz (187912,240)I (188152,220)made (188372,220)more (188592,230)on (188822,230)my (189052,220)second (189272,150)leg(189422,600)\n        [190022,4150]Motherf**kin' (190022,600)Birdman (190622,570)junior' (191192,830)eleventh (192022,590)grade(192612,1560)\n        [194172,1699]Ball (194172,240)on (194412,240)automatic (194652,589)start(195241,630)\n        [195871,3290]I (195871,330)can (196201,270)hand (196471,230)it (196701,240)to (196941,230)Drake (197171,210)or (197381,210)do (197591,200)a (197791,240)quarterback (198031,280)draw(198311,850)\n        [199161,3620]Wildcat (199161,630)offense   (199791,1360)Check (201151,280)the (201431,220)paw (201651,370)prints  (202021,760)\n        [202781,2190]We (202781,280)in (203061,290)da (203351,280)buildin'' (203631,270)you (203901,380)niggas (204281,250)in (204531,250)appartments(204781,1530)\n        [206311,2500]Ugh  (206311,460)N-now (206771,540)c'mon (207311,240)be (207551,240)my (207791,250)blood (208041,370)doner(208411,400)\n        [208811,1620]My (208811,360)flow (209171,250)so (209421,290)nice (209711,220)you(209931,500)\n        [210431,2190]Ain't (210431,240)even (210671,220)gotta (210891,240)put (211131,260)a (211391,230)rug (211621,230)on (211851,360)her(212211,410)\n        [212621,2240]Do (212621,200)it (212821,210)big (213031,220)and (213251,230)let (213481,220)the (213701,220)small (213921,210)fall (214131,230)under (214361,210)that(214571,290)\n        [214861,1930]Damn (214861,290)where (215151,230)you (215381,350)stumbled (215731,240)at (215971,820)\n        [216791,2090]From (216791,230)where (217021,350)did (217371,270)they (217641,280)gumbo (217921,310)at (218231,650)\n        [218881,2690]Came (218881,260)got (219141,250)the (219391,310)beat (219701,310)jumpin' (220011,230)like (220241,300)a (220541,310)jumpin' (220851,280)jack(221131,440)\n        [221571,1080]And (221571,220)you (221791,230)know (222021,360)me(222381,629)\n        [223010,2040]I (223010,270)get (223280,190)on (223470,210)this (223680,210)b**ch (223890,190)and (224080,200)have (224280,180)a (224460,180)heart (224640,200)attack(224840,210)\n        [225050,1370]Hip-Hop (225050,180)I'm (225230,200)the (225430,210)heart (225640,240)of (225880,260)that(226140,280)\n        [226420,1410]Ni**a (226420,250)nothin' (226670,270)short (226940,250)of (227190,300)that(227490,340)\n        [227830,4449]President (227830,660)Carter' (228490,870)Young (229360,300)Money (229660,430)Democrat  (230090,1489)Ugh (231579,700)\n        [232279,1680]Now (232279,250)tell (232529,220)me (232749,230)how (232979,220)you (233199,220)love (233419,220)it(233639,320)\n        [233959,1340]You (233959,210)know (234169,240)you (234409,230)at (234639,230)the (234869,200)top(235069,230)\n        [235299,3120]And (235299,200)all (235499,210)of (235709,240)heaven's (235949,260)right (236209,200)above (236409,210)it (236619,210)we (236829,210)on  (237039,1380)\n        [238419,970]It's (238419,220)young (238639,240)money (238879,250)motherf**ker(239129,809)\n        [239938,1640]If (239938,260)you (240198,230)ain't (240428,220)runnin' (240648,550)wit(241198,380)\n        [241578,2400]It (241578,280)run (241858,260)from (242118,250)it (242368,250)motherf**ker (242618,230)alright(242848,1130)\n        [243978,2690]Now (243978,240)somebody (244218,270)show (244488,300)some (244788,300)money (245088,240)in (245328,290)this (245618,270)b**ch(245888,780)\n        [246668,1600]And (246668,290)I (246958,190)got (247148,230)my (247378,240)bees (247618,200)with (247818,220)me(248038,230)\n        [248268,3230]Like (248268,350)some (248618,210)honey (248828,220)in (249048,200)this (249248,180)b**ch' (249428,210)ya (249638,220)dig  (249858,1640)\n        [251498,2720]I (251498,240)got (251738,220)my (251958,260)gun (252218,260)in (252478,290)my (252768,250)boo (253018,350)purse(253368,769)\n        [254137,4540]And (254137,340)I (254477,230)don't (254707,220)bust (254927,200)back' (255127,200)because (255327,190)I (255517,210)shoot (255727,220)first  (255947,2730)\n        [258677,10890]Yeah    (258677,3800)We (262477,270)on    (262747,3920)Young (266667,400)moola (267067,730)baby (267797,1770)\n        \"/>\n    </LyricInfo>\n</QrcInfos>";

            string regEx = "(\\s+[\\w:.-]+\\s*=\\s*\")(([^\"]*)((\")((?!\\s+[\\w:.-]+\\s*=\\s*\"|\\s*(?:/?|\\?)>))[^\"]*)*)\"";

            StringBuilder sb = new StringBuilder();

            int currentPos = 0;
            foreach(System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(text, regEx)) {
                sb.Append(text.Substring(currentPos, match.Index - currentPos));
                string f = match.Result(match.Groups[1].Value + match.Groups[2].Value.Replace("\"", "&quot;")) + "\"";
                sb.Append(f);
                currentPos = match.Index + match.Length;
            }

            sb.Append(text.Substring(currentPos));

            Console.Write(sb.ToString());
        }

    }
}